package android.theHead;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class TeamView extends Activity{
	Context thisContext;
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		
		thisContext = this;
		
		setContentView(R.layout.team_details);
		
		Intent receivedIntent = this.getIntent();
		int teamID = receivedIntent.getIntExtra("teamID", 0);
		
		TeamDBAdapter teamDB = new TeamDBAdapter(this);
        teamDB.open();
        
        Cursor c = teamDB.getTeamInfo(teamID);
        c.moveToLast();
        int drawable = getResources().getIdentifier(c.getString(c.getColumnIndex("clubID")).toLowerCase(), "drawable", "android.theHead");
        ((ImageView) findViewById(R.id.TeamView_icon)).setImageResource(drawable);
        ((TextView) findViewById(R.id.TeamView_division)).setText(c.getString(c.getColumnIndex("division")));
        ((TextView) findViewById(R.id.TeamView_name)).setText(c.getString(c.getColumnIndex("teamName")));
        ((TextView) findViewById(R.id.TeamView_club_name)).setText(c.getString(c.getColumnIndex("clubName")));
        ((TextView) findViewById(R.id.TeamView_club_city)).setText(c.getString(c.getColumnIndex("clubCity")) + " (" + c.getString(c.getColumnIndex("clubCountry")) +")");
        String rower;
        rower = c.getString(c.getColumnIndex("stroke"));
        if (rower.length()>0){
        	((TextView) findViewById(R.id.TeamView_stroke)).setText(rower);
        } else {
        	((TextView) findViewById(R.id.TeamView_stroke)).setVisibility(View.GONE);
        }
        rower = c.getString(c.getColumnIndex("rower_7"));
        if (rower.length()>0){
        	((TextView) findViewById(R.id.TeamView_rower7)).setText(rower);
        } else {
        	((TextView) findViewById(R.id.TeamView_rower7)).setVisibility(View.GONE);
        }
        rower = c.getString(c.getColumnIndex("rower_6"));
        if (rower.length()>0){
        	((TextView) findViewById(R.id.TeamView_rower6)).setText(rower);
        } else {
        	((TextView) findViewById(R.id.TeamView_rower6)).setVisibility(View.GONE);
        }
        rower = c.getString(c.getColumnIndex("rower_5"));
        if (rower.length()>0){
        	((TextView) findViewById(R.id.TeamView_rower5)).setText(rower);
        } else {
        	((TextView) findViewById(R.id.TeamView_rower5)).setVisibility(View.GONE);
        }
        rower = c.getString(c.getColumnIndex("rower_4"));
        if (rower.length()>0){
        	((TextView) findViewById(R.id.TeamView_rower4)).setText(rower);
        } else {
        	((TextView) findViewById(R.id.TeamView_rower4)).setVisibility(View.GONE);
        }
        rower = c.getString(c.getColumnIndex("rower_3"));
        if (rower.length()>0){
        	((TextView) findViewById(R.id.TeamView_rower3)).setText(rower);
        } else {
        	((TextView) findViewById(R.id.TeamView_rower3)).setVisibility(View.GONE);
        }
        rower = c.getString(c.getColumnIndex("rower_2"));
        if (rower.length()>0){
        	((TextView) findViewById(R.id.TeamView_rower2)).setText(rower);
        } else {
        	((TextView) findViewById(R.id.TeamView_rower2)).setVisibility(View.GONE);
        }
        rower = c.getString(c.getColumnIndex("bow"));
        if (rower.length()>0){
        	((TextView) findViewById(R.id.TeamView_bow)).setText(rower);
        } else {
        	((TextView) findViewById(R.id.TeamView_bow)).setVisibility(View.GONE);
        }
        ((TextView) findViewById(R.id.TeamView_cox)).setText(c.getString(c.getColumnIndex("cox")));
        ((TextView) findViewById(R.id.TeamView_coaches)).setText(c.getString(c.getColumnIndex("coaches")));
        ((TextView) findViewById(R.id.TeamView_boat)).setText(c.getString(c.getColumnIndex("boatName")));
        
        c.close();
        teamDB.close();
	}
	
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.spectator_menu, menu);
        menu.removeItem(R.id.gotoRowMode);
        menu.removeItem(R.id.gotoQuit);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	Intent intent;
    	
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.gotoMapView:
        	intent = new Intent(thisContext, ViewMap.class);
		    thisContext.startActivity(intent);
        	return true;
        case R.id.gotoRowMode:
			intent = new Intent(thisContext, RowmodeIntro.class);
			thisContext.startActivity(intent);
			return true;
        case R.id.gotoListView:
        	intent = new Intent(thisContext, Overview.class);
		    thisContext.startActivity(intent);
        	return true;
        case R.id.gotoQuit:
			//TODO goes backto previous Activity
			finish();
        default:
            return super.onOptionsItemSelected(item);
        }
    }
}
